﻿/*
 *写一个订单管理的控制台程序，能够实现添加订单、删除订单、修改订单、查询订单（按照订单号、商品名称、客户等字段进行查询）功能。
 *提示：主要的类有Order（订单）、OrderItem（订单明细项），OrderService（订单服务），订单数据可以保存在OrderService中一个List中。在Program里面可以调用OrderService的方法完成各种订单操作。
 *要求：
 *（1）使用LINQ语言实现各种查询功能，查询结果按照订单总金额排序返回。👌
 *（2）在订单删除、修改失败时，能够产生异常并显示给客户错误信息。👌
 *（3）作业的订单和订单明细类需要重写Equals方法，确保添加的订单不重复，每个订单的订单明细不重复。👌
 *（4）订单、订单明细、客户、货物等类添加ToString方法，用来显示订单信息。👌
 *（5）OrderService提供排序方法对保存的订单进行排序。默认按照订单号排序，也可以使用Lambda表达式进行自定义排序。👌
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderControl
{
    class Program
    {
        static void Main(string[] args)
        {
            Item apple = new Item(0, "苹果", 100, 100);
            Item.ItemList.Add(apple);
            Item orange = new Item(1, "橘子", 50, 100);
            Item.ItemList.Add(orange);
            Item computer = new Item(2, "电脑", 5000, 100);
            Item.ItemList.Add(computer);
            OrderItem orderItem = new OrderItem();
            Order order = new Order();
            bool login = false;
            string username;
            Console.WriteLine("请输入你的用户名登陆：");
            username = Console.ReadLine();
            while (username == "")
            {
                Console.WriteLine("请输入你的用户名登陆：");
                username = Console.ReadLine();
            }
            login = true;
            while (login)
            {
                int case_ = 6;
                Console.WriteLine("请选择你要做的事:");
                Console.WriteLine("\t0 for 选择商品");
                Console.WriteLine("\t1 for 添加订单");
                Console.WriteLine("\t2 for 查询订单");
                Console.WriteLine("\t3 for 删除订单");
                Console.WriteLine("\t4 for 退出登录");
                string case__ = Console.ReadLine();
                while(!int.TryParse(case__ ,out case_))
                {
                    Console.WriteLine("请重新选择你要做的事:");
                    case__ = Console.ReadLine();
                }
                switch (case_)
                {
                    case 0:
                        bool stop = false;
                        while (!stop)
                        {
                            int next = 100;
                            int number = 0;
                            string next_ = "";
                            string number_ = "";
                            Console.WriteLine("\n商品号 - Name - Price - 库存");
                            foreach (Item i in Item.ItemList)
                            {
                                Console.WriteLine(i.ToString());
                            }
                            Console.WriteLine("请选择你想添加的商品，并输入商品号");
                            next_ = Console.ReadLine();
                            while (!int.TryParse(next_, out next))
                            {
                                Console.WriteLine("请重新选择你要的商品:");
                                next_ = Console.ReadLine();
                            }
                            Console.WriteLine("请输入想购买的数量：");
                            number_ = Console.ReadLine();
                            while (!int.TryParse(number_, out number))
                            {
                                Console.WriteLine("请重新输入数量:");
                                number_ = Console.ReadLine();
                            }
                            switch (next)
                            {
                                case 0:
                                    orderItem.addItem(apple, number);
                                    Console.WriteLine("\t已选择"+apple.Name+" * "+number);
                                    break;
                                case 1:
                                    orderItem.addItem(orange, number);
                                    Console.WriteLine("\t已选择" + orange.Name + " * " + number);
                                    break;
                                case 2:
                                    orderItem.addItem(computer, number);
                                    Console.WriteLine("\t已选择" + computer.Name + " * " + number);
                                    break;
                                default:
                                    Console.WriteLine("不存在此商品");
                                    break;
                            }
                            Console.WriteLine("退出输入1，否则继续选择");
                            string stop_ = Console.ReadLine();
                            if(stop_ == "1")
                            {
                                stop = true;
                            }
                        }
                        break;
                    case 1:
                        order = new Order(orderItem, username);
                        if (OrderService.AddOrder(order))
                        {
                            Console.WriteLine("订单添加成功");
                            orderItem = new OrderItem();
                        }
                        else
                        {
                            Console.WriteLine("订单添加失败");
                        }           
                        break;
                    case 2:
                        OrderService.checkOrder(username);
                        break;
                    case 3:
                        Console.WriteLine("输入你想删除的订单编号:");
                        string orderNumber_ = Console.ReadLine();
                        int orderNumber = int.MaxValue;
                        while(!int.TryParse(orderNumber_,out orderNumber))
                        {
                            Console.WriteLine("输入你想删除的订单编号:");
                            orderNumber_ = Console.ReadLine();
                        }
                        OrderService.DeleteOrder(orderNumber);
                        Console.WriteLine("编号为"+orderNumber+"的订单已删除");
                        break;
                    case 4:
                        login = false;
                        break;
                }
            }
        }
    }
}
